<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace app\manage\controller\admin;

use app\common\command\Crontab;
use app\manage\model\admin\SysCrontab;
use buwang\base\AdminBaseController;
use buwang\service\CacheService;
use buwang\traits\CrudControllerTrait;


class SysCrontabController extends AdminBaseController
{
    use CrudControllerTrait;

    protected $model = null;//模型实例

    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 定时任务名称及标识-须自定义
     */
    public function cornName()
    {
        $data = [
            'corn_test' => '测试定时任务',
        ];
        return $this->success('successful', $data);
    }

    /**
     * 定时任务列表
     */
    public function lists()
    {
        $page = request()->get('page/d', 1);
        $limit = request()->get('limit/d', 10);

        $where['is_del'] = 0;
        $lists = SysCrontab::where($where)
            ->append(['is_open_desc'])
            ->order(['id' => 'desc'])
            ->page($page, $limit)
            ->select()
            ->toArray();

        foreach ($lists as $key => &$value) {
            switch ($value['type']) {
                case 1:
                    $value['execute_cycle'] = '每隔' . $value['cycle'] . '分钟执行';
                    break;
                case 2:
                    $arr = explode('/', $value['cycle']);
                    $value['execute_cycle'] = '每' . $arr[0] . '小时, 第' . $arr[1] . '分钟 执行';
                    break;
                case 3:
                    $value['execute_cycle'] = '每小时, 第' . $value['cycle'] . '分钟执行';
                    break;
                case 4:
                    $arr = explode('/', $value['cycle']);
                    $value['execute_cycle'] = '每天, ' . $arr[0] . '点' . $arr[1] . '分 执行';
                    break;
                case 5:
                    $arr = explode('/', $value['cycle']);
                    $value['execute_cycle'] = '每隔' . $arr[0] . '天, ' . $arr[1] . '点' . $arr[2] . '分 执行';
                    break;
                case 6:
                    $arr = explode('/', $value['cycle']);
                    $value['execute_cycle'] = '每周' . $arr[0] . ', ' . $arr[1] . '点' . $arr[2] . '分执行';
                    break;
                case 7:
                    $arr = explode('/', $value['cycle']);
                    $value['execute_cycle'] = '每月' . $arr[0] . '日 ' . $arr[1] . '点' . $arr[2] . '分执行';
                    break;
                case 8:
                    $arr = explode('/', $value['cycle']);
                    $value['execute_cycle'] = '每年' . $arr[0] . '月' . $arr[1] . '日 ' . $arr[2] . '点' . $arr[3] . '分执行';
                    break;
                default:
                    $value['execute_cycle'] = '周期有误！';
                    break;
            }
        }

        $data = [
            'lists' => $lists,
            'count' => SysCrontab::where($where)->count(),
        ];

        return $this->success('successful', $data);
    }

    /**
     * 添加定时任务
     */
    public function add()
    {
        $params = request()->post();
        $rule = [
            'corn_name' => 'require',//定时任务名称
            'corn_mark' => 'require',//定时任务命令
            'corn_type' => 'require',//周期状态 1=N分钟 2=N小时 3=每小时 4=每天 5=N天 6=每星期 7=每月 8=每年
            'corn_cycle' => 'require',//执行周期
            'corn_is_open' => 'require|in:0,1',//是否开启:0=关闭,1=开启
        ];
        $this->validate($params, $rule);


        $sysCrontab = SysCrontab::where('name', $params['corn_name'])->find();
        if ($sysCrontab) {
            return $this->error('该定时任务已经存在');
        }
        $data = [
            'name' => $params['corn_name'],//定时任务名称
            'mark' => $params['corn_mark'],//标识
            'type' => $params['corn_type'],//周期状态 1=N分钟 2=N小时 3=每小时 4=每天 5=N天 6=每星期 7=每月 8=每年
            'cycle' => $params['corn_cycle'],//执行周期
            'is_open' => $params['corn_is_open'],//是否开启:0=关闭,1=开启
            'title' => $params['corn_title'],//描述
        ];

        SysCrontab::create($data);

        $lists = SysCrontab::where('is_open', 1)->select()->toArray();
        CacheService::set('sys_crontab_key_mp', $lists);

        return $this->success('successful');
    }

    /**
     *  查看定时任务详情
     */
    public function detail()
    {
        $id = request()->get('id/d', 0);
        if (!$id) {
            return $this->error('缺少参数');
        }

        $sysCrontab = SysCrontab::where('id', $id)->append(['is_open_desc'])->find();
        if (!$sysCrontab) {
            return $this->error('该定时任务不存在');
        }
        return $this->success('successful', $sysCrontab->toArray());
    }

    /**
     * 编辑定时任务
     */
    public function edit()
    {
        $params = request()->post();
        $rule = [
            'id' => 'require',
            'corn_name' => 'require',//定时任务名称
            'corn_mark' => 'require',//标识
            'corn_type' => 'require',//周期状态 1=N分钟 2=N小时 3=每小时 4=每天 5=N天 6=每星期 7=每月 8=每年
            'corn_cycle' => 'require',//执行周期
            'corn_is_open' => 'require|in:0,1',//是否开启:0=关闭,1=开启
        ];
        $this->validate($params, $rule);

        $sysCrontab = SysCrontab::where('id', $params['id'])->find();
        if (!$sysCrontab) {
            return $this->error('该定时任务不存在');
        }
        $data = [
            'id' => $params['id'],//定时任务名称
            'name' => $params['corn_name'],//定时任务名称
            'mark' => $params['corn_mark'],//标识
            'type' => $params['corn_type'],//周期状态 1=N分钟 2=N小时 3=每小时 4=每天 5=N天 6=每星期 7=每月 8=每年
            'cycle' => $params['corn_cycle'],//执行周期
            'is_open' => $params['corn_is_open'],//是否开启:0=关闭,1=开启
            'title' => $params['corn_title'],//描述
        ];

        SysCrontab::update($data);

        $lists = SysCrontab::where('is_open', 1)->select()->toArray();
        CacheService::set('sys_crontab_key_mp', $lists);

        return $this->success('successful');
    }

    /**
     * 删除定时任务
     */
    public function delete()
    {
        $params = request()->post();
        $rule = [
            'id' => 'require',
        ];
        $this->validate($params, $rule);

        SysCrontab::destroy($params['id']);

        $lists = SysCrontab::where('is_open', 1)->select()->toArray();
        CacheService::set('sys_crontab_key_mp', $lists);

        return $this->success('successful');
    }

    /**
     * 修改显示状态
     */
    public function setIsOpen()
    {
        $params = request()->post();
        $rule = [
            'id' => 'require',
            'is_open' => 'require',
        ];
        $this->validate($params, $rule);

        SysCrontab::update($params);

        $lists = SysCrontab::where('is_open', 1)->select()->toArray();
        CacheService::set('sys_crontab_key_mp', $lists);

        return $this->success('successful');
    }

}
